// https://leetcode.cn/problems/merge-sorted-array/?envType=study-plan-v2&envId=top-interview-150

class Solution {
public:
    void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
        int i = 0, j = 0, p = 0;
        vector<int> res(m + n);

        while (i < m && j < n) {
            if (nums1[i] <= nums2[j]) {
                res[p] = nums1[i];
                i++;
            } else {
                res[p] = nums2[j];
                j++;
            }
            p++;
        }

        while (i < m) {
            res[p] = nums1[i];
            p++;
            i++;
        }
        while (j < n) {
            res[p] = nums2[j];
            p++;
            j++;
        }

        for (int k = 0; k < m + n; k++) {
            nums1[k] = res[k];
        }
    }
};